//
//  NSString+Extansion.m
//  HeylinkPro
//
//  Created by 胡韩国 on 2023/2/22.
//  Copyright © 2023 鼎合丰. All rights reserved.
//

#import "NSString+Extansion.h"

@implementation NSString (Extansion)

- (BOOL)isEmail{
    
    NSString *emailRegex = @"^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:self];
}

- (BOOL)isPhone{
    
    NSString *MOBILE = @"^1(1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|8[0-9]|7[0-9]|6[0-9]|9[0-9])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    return [regextestmobile evaluateWithObject:self];
}

#pragma mark - 富文本
+ (NSAttributedString *)getAttributeWith:(id)sender
                                  string:(NSString *)string
                               orginFont:(UIFont *)orginFont
                              orginColor:(UIColor *)orginColor
                           attributeFont:(UIFont *)attributeFont
                          attributeColor:(UIColor *)attributeColor
{
    __block  NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:string];
    [totalStr addAttribute:NSFontAttributeName value:orginFont range:NSMakeRange(0, string.length)];
    [totalStr addAttribute:NSForegroundColorAttributeName value:orginColor range:NSMakeRange(0, string.length)];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5.0f]; //设置行间距
    [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
    [paragraphStyle setAlignment:NSTextAlignmentLeft];
    [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
    [totalStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
    
    if ([sender isKindOfClass:[NSArray class]]) {
        __block NSString *oringinStr = string;
        [sender enumerateObjectsUsingBlock:^(NSString *  _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSRange range = [oringinStr rangeOfString:str];
            [totalStr addAttribute:NSFontAttributeName value:attributeFont range:range];
            [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
            if(range.length>0){
                oringinStr = [oringinStr stringByReplacingCharactersInRange:range withString:[[self class] getStringWithRange:range]];
            }
        }];
    }else if ([sender isKindOfClass:[NSString class]]) {
        NSRange range = [string rangeOfString:sender];
        [totalStr addAttribute:NSFontAttributeName value:attributeFont range:range];
        [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
    }
    return totalStr;
}

+ (NSString *)getStringWithRange:(NSRange)range
{
    NSMutableString *string = [NSMutableString string];
    for (int i = 0; i < range.length ; i++) {
        [string appendString:@" "];
    }
    return string;
}

-(NSString *)transformMacAddress{
    if (self.length == 0) {
        return @"";
    }
   
    if ([self containsString:@":"]) {
        return self;
    }
    NSString *newString = [self uppercaseString];
    NSMutableString *macadd = newString.mutableCopy;
    
    for (NSInteger i = macadd.length - 2; i > 0; i--) {
        if (i%2 == 0) {
            [macadd insertString:@":" atIndex:i];
        }
    }
    return macadd;
}

- (NSString*)replaceSubString{
    NSInteger strLength = self.length;
    if (strLength<=8) {
        return self;
    }
    
    NSMutableString * withstr = [NSMutableString stringWithString:@""];
    for (int i = 0; i < strLength - 7 ; i ++)
    {
        [withstr appendString:@"*"];
    }
    
    NSString *replaceStr =  [self stringByReplacingCharactersInRange:NSMakeRange(3,  strLength - 7) withString:withstr];
    return replaceStr;
    
}

//计算文字长度
- (CGSize)sizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize {
    NSDictionary *attrs = @{NSFontAttributeName : font};
    return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
}

- (NSString*)currentLanguage{
    NSString * requstLanguage = @"CN";
    if([self hasPrefix:@"zh"]){
        requstLanguage = @"CN";
    }else if ([self hasPrefix:@"en"]){
        requstLanguage = @"US";
    }else if ([self hasPrefix:@"es"]){
        requstLanguage = @"ES";
    }else if ([self hasPrefix:@"fr"]){
        requstLanguage = @"FR";
    }else if ([self hasPrefix:@"de"]){
        requstLanguage = @"DE";
    }else if ([self hasPrefix:@"ja"]){
        requstLanguage = @"JA";
    }else if ([self hasPrefix:@"ko"]){
        requstLanguage = @"KO";
    }else if ([self hasPrefix:@"ru"]){
        requstLanguage = @"RU";
    }else if ([self hasPrefix:@"pt"]){
        requstLanguage = @"PT";
    }else if ([self hasPrefix:@"ar"]){
        requstLanguage = @"AR";
    }else if ([self hasPrefix:@"it"]){
        requstLanguage = @"IT";
    }else if ([self hasPrefix:@"nl"]){
        requstLanguage = @"NL";
    }else if ([self hasPrefix:@"da"]){
        requstLanguage = @"DA";
    }else if ([self hasPrefix:@"hi"]){
        requstLanguage = @"HI";
    }else if ([self hasPrefix:@"ms"]){
        requstLanguage = @"MS";
    }else if ([self hasPrefix:@"th"]){
        requstLanguage = @"TH";
    }else if ([self hasPrefix:@"ne"]){
        requstLanguage = @"NE";
    }else if ([self hasPrefix:@"bn"]){
        requstLanguage = @"BN";
    }else if ([self hasPrefix:@"km"]){
        requstLanguage = @"KM";
    }else if ([self hasPrefix:@"el"]){
        requstLanguage = @"EL";
    }else{
        requstLanguage = @"US";
    }
    return requstLanguage;
}

- (NSString*)uiLanguage{
    NSString * requstLanguage = @"CN";
    if([self hasPrefix:@"zh"]){
        requstLanguage = @"zh";
    }else if ([self hasPrefix:@"en"]){
        requstLanguage = @"en";
    }else if ([self hasPrefix:@"es"]){
        requstLanguage = @"ES";
    }else if ([self hasPrefix:@"fr"]){
        requstLanguage = @"FR";
    }else if ([self hasPrefix:@"de"]){
        requstLanguage = @"DE";
    }else if ([self hasPrefix:@"ja"]){
        requstLanguage = @"JA";
    }else if ([self hasPrefix:@"ko"]){
        requstLanguage = @"KO";
    }else if ([self hasPrefix:@"ru"]){
        requstLanguage = @"RU";
    }else if ([self hasPrefix:@"pt"]){
        requstLanguage = @"PT";
    }else if ([self hasPrefix:@"ar"]){
        requstLanguage = @"AR";
    }else if ([self hasPrefix:@"it"]){
        requstLanguage = @"IT";
    }else if ([self hasPrefix:@"nl"]){
        requstLanguage = @"NL";
    }else if ([self hasPrefix:@"da"]){
        requstLanguage = @"DA";
    }else if ([self hasPrefix:@"hi"]){
        requstLanguage = @"HI";
    }else if ([self hasPrefix:@"ms"]){
        requstLanguage = @"MS";
    }else if ([self hasPrefix:@"th"]){
        requstLanguage = @"TH";
    }else if ([self hasPrefix:@"ne"]){
        requstLanguage = @"NE";
    }else if ([self hasPrefix:@"bn"]){
        requstLanguage = @"BN";
    }else if ([self hasPrefix:@"km"]){
        requstLanguage = @"KM";
    }else if ([self hasPrefix:@"el"]){
        requstLanguage = @"EL";
    }else{
        requstLanguage = @"US";
    }
    return requstLanguage;
}

//随机字符串
+ (NSString*)randomString{

    int len = (arc4random() % 1) + 9;
    char* charSet = "CCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

    char* temp = malloc(12 + 1);

    for (int i = 0; i < 12; i++) {

        int randomPoz = arc4random()%strlen(charSet);

        temp[i] = charSet[randomPoz];
    }
    temp[len] = '\0';

    NSMutableString* randomString = [[NSMutableString alloc] initWithUTF8String:temp];

    free(temp);

    return randomString;

}


@end
